
package com.yimixian.app.rest.api;

import android.text.TextUtils;

import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.HotTips;
import com.yimixian.app.model.SearchKeyResult;
import com.yimixian.app.util.SharedPreferencesHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by szy on 2016/1/19.
 */
public class HotTipsAPI extends YmxBaseAPI<HotTips> {

    public static final String GET_HOT_TIPS = "/v5/search/page";

    /**
     * @param callback
     */
    public HotTipsAPI(Callback<Response> callback) {
        super(callback);
    }

    @Override
    public String getRelativeURL() {
        return GET_HOT_TIPS;
    }

    public void getHotTips() {
        Address address = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        String storeIdStr = "";
        if (address != null) {
            storeIdStr = address.storeId;
        }
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        int storeId = 0;
        if(!TextUtils.isEmpty(storeIdStr)) {
            storeId = Integer.parseInt(storeIdStr);
        }
        if(TextUtils.isEmpty(token)) {
            token = "";
        }
        mYmxRestService.getHotTips(storeId,token,mCallback);
    }

    @Override
    public HotTips parseJson(JSONObject jsonObject) {
      HotTips hotTips = new HotTips();
      hotTips.hotTips = new ArrayList<>();
      if(jsonObject != null) {
          JSONArray jsonArray = jsonObject.optJSONArray("hot_key_word");
          if(jsonArray != null) {
              for(int i=0;i<jsonArray.length();i++) {
                  String hotKeyWord = jsonArray.optString(i);
                  if(!TextUtils.isEmpty(hotKeyWord)) {
                      hotTips.hotTips.add(hotKeyWord);
                  }
              }
          }
      }
      return hotTips;
    }
}
